//
//  ReviewViewController.m
//  Streaming
//
//  Created by 官琛 on 15-6-23.
//  Copyright (c) 2015年 liuxun. All rights reserved.
//

#import "ReviewViewController.h"

@interface ReviewViewController ()

@end

@implementation ReviewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self afnGetRequest];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    
    [_tableView autorelease];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellInfer = @"reviewCell";
    ReviewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInfer];
    if (cell == nil) {
        cell = [[ReviewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellInfer];
    }
    Review *revi = [self.array objectAtIndex:indexPath.row];
    cell.rev = revi;
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}

-(void)afnGetRequest
{
    
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
   NSString *url_string = [NSString stringWithFormat:@"http://api.m.mtime.cn/Showtime/MovieDetail.api?locationId=729&movieId=%ld",self.movieID];
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];
    
    [manager.requestSerializer setValue:@"_tt_=558a4bb6344c614089b3c177; Hm_lvt_e07949f61338e36deb5fc85107e6157b=1435021354,1435022253,1435111209,1435126765; Hm_lpvt_e07949f61338e36deb5fc85107e6157b=1435150513" forHTTPHeaderField:@"Cookie"];
    
    [manager.requestSerializer setValue:@"6,1435280866380,701bf358599a23b25be279d96303d90c,xiaomi" forHTTPHeaderField:@"X-Mtime-Mobile-CheckValue"];
    
    
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
//    NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        
        NSMutableArray *array = [NSMutableArray array];
        array = [responseObject objectForKey:@"cts"];
        self.array = [NSMutableArray array];
        for (NSMutableDictionary *dic in array) {
            Review *rev = [[Review alloc]init];
            rev.ce = [dic objectForKey:@"ce"];
            rev.ca = [dic objectForKey:@"ca"];
            rev.cal = [dic objectForKey:@"cal"];
            rev.caimg = [dic objectForKey:@"caimg"];
            rev.cr = [[dic objectForKey:@"cr"]integerValue];
            [self.array addObject:rev];
            [rev release];
        }
        NSLog(@"aaaaaa = %ld",self.array.count);
        [self.tableView reloadData];
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
      
//        [self.HUD hide:YES];
    }];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
